package com.atguigu.lease.web.app.service.impl;


import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.common.utils.JwtUtil;
import com.atguigu.lease.model.entity.UserInfo;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.app.mapper.UserInfoMapper;
import com.atguigu.lease.web.app.service.LoginService;
import com.atguigu.lease.web.app.sms.SmsConfiguration;
import com.atguigu.lease.web.app.vo.user.LoginVo;
import com.atguigu.lease.web.app.vo.user.UserInfoVo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import io.jsonwebtoken.Claims;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private SmsConfiguration smsConfiguration;

    @Autowired
    UserInfoMapper userInfoMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;



    @Override
    public void getSMSCode(String phone) {
       smsConfiguration.generateAndSendCode(phone);
    }

    @Transactional
    @SneakyThrows
    @Override
    public String login(LoginVo loginVo) {
        if (!StringUtils.hasText(loginVo.getPhone())) {
            throw new Exception(ResultCodeEnum.APP_LOGIN_PHONE_EMPTY.getMessage());
        }

        if (!StringUtils.hasText(loginVo.getCode())) {
            throw new Exception(ResultCodeEnum.APP_LOGIN_CODE_EMPTY.getMessage());
        }
        String userCode = loginVo.getCode();
        String userPhone = loginVo.getPhone();
        String key = RedisConstant.APP_LOGIN_PREFIX + userPhone;
        String code = redisTemplate.opsForValue().get(key);
        Assert.notNull(code, "验证码已过期");
        Assert.isTrue(userCode.equals(code), "验证码错误");
        redisTemplate.delete(key);

        UserInfo userInfo = userInfoMapper.selectByPhone(userPhone);
        if (userInfo == null) {
            userInfo = new UserInfo();
            userInfo.setPhone(loginVo.getPhone());
            userInfo.setStatus(BaseStatus.ENABLE);
            userInfo.setNickname("用户-"+loginVo.getPhone().substring(5));
            userInfoMapper.insert(userInfo);
        }

        //4.判断用户是否被禁
        if (userInfo.getStatus().equals(BaseStatus.DISABLE)) {
            throw new Exception(ResultCodeEnum.APP_ACCOUNT_DISABLED_ERROR.getMessage());
        }

        //5.创建并返回TOKEN
        return JwtUtil.createAppToken(userInfo.getId(), loginVo.getPhone());
    }

    @Override
    public UserInfoVo info(String token) {
        Claims claims = JwtUtil.parseToken(token);
        Long userId = claims.get("userId",Long.class);
        UserInfo userInfo = userInfoMapper.selectById(userId);
        UserInfoVo userInfoVo = new UserInfoVo(userInfo.getNickname(),userInfo.getAvatarUrl());
        return userInfoVo;
    }


}
